package com.atguigu.common.result;/*
 * @author: XueYouPeng
 * @time: 2023/6/2 20:45
 */


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;
import java.util.Map;

//各个接口的统一返回值
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "公共返回结果")
public class Result {

    @ApiModelProperty(name = "com/atguigu/code",value = "返回自定义状态码")
    private Integer code;// 20000 成功  ， 20001失败
    @ApiModelProperty(name = "message",value = "自定义提示消息")
    private String message;//操作成功，添加成功，删除成功，删除失败，操作失败.....
    @ApiModelProperty(name = "success",value = "是否成功")
    private Boolean success;//true  false
    @ApiModelProperty(name = "data",value = "返回的业务数据")
    private Map<String,Object> data = new HashMap<>();//data表示返回的业务数据，通常查询接口会使用这个属性；

    /*
     * 通过ok方法返回一个通用的操作成功的r对象，data没有赋值
     * @author: XueYouPeng
     * @time: 2023/6/2 20:47
     */
     public static Result ok(){
         Result result = new Result();
         result.setSuccess(true);
         result.setCode(ResultCode.SUCCESS);
         result.setMessage("操作成功");
         return result;
     }

     /*
      * 通过ok方法返回一个通用的操作成功的r对象，data没有赋值
      * @author: XueYouPeng
      * @time: 2023/6/2 20:49
      */
      public static Result error(){
          Result result = new Result();
          result.setSuccess(true);
          result.setCode(ResultCode.ERROR);
          result.setMessage("操作失败");
          return result;
      }

    //获取r对象之后，修改其中的属性值
    public Result code(Integer code){
        this.setCode(code);
        return this;
    }
    public Result message(String message){
        this.setMessage(message);
        return this;
    }
    public Result success(Boolean success){
        this.setSuccess(success);
        return this;
    }
    public Result data(String key,Object value){
        this.getData().put(key,value);
        return this;
    }
    public Result data(Map<String,Object> data){
        this.setData(data);
        return this;
    }

}
